package com.gouldzd.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.gouldzd.dao.CheckItemDao;
import com.gouldzd.entity.PageResult;
import com.gouldzd.entity.QueryPageBean;
import com.gouldzd.pojo.CheckItem;
import com.gouldzd.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/*
 * @Author zhangdan
 * @Description //TODO 检查项服务
 * @Date 9:56 2020/4/28
 * @Param 
 * @return 
 **/
@Service(interfaceClass = CheckItemService.class)//加了事务注解需加权限类名
@Transactional
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemDao checkItemDao;

    //新增检查项
    @Override
    public void add(CheckItem checkItem) {
        checkItemDao.add(checkItem);
    }

    //检查项分页条件查询
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();//当前页
        Integer pageSize = queryPageBean.getPageSize();//每页显示条数
        String queryString = queryPageBean.getQueryString();//查询条件

        //完成分页查询,基于Mybatis框架提供的分页助手插件完成
        PageHelper.startPage(currentPage,pageSize);

        Page<CheckItem> page = checkItemDao.selectByCondition(queryString);
        long total = page.getTotal();
        List<CheckItem> rows = page.getResult();

        return new PageResult(total,rows);
    }

    //根据id删除检查项
    @Override
    public void deleteById(Integer id) {
        //判断当前检查项是否已经关联到检查组
        long count = checkItemDao.findCountByCheckItemId(id);
        if(count > 0){
            //当前检查项已经被关联到检查组,不允许删除
            new RuntimeException();
        }
        checkItemDao.deleteById(id);
    }

    //根据id查询检查项
    @Override
    public CheckItem findById(Integer id) {
        return checkItemDao.findById(id);
    }

    //根据id修改检查项
    @Override
    public void edit(CheckItem checkItem) {
        checkItemDao.updateById(checkItem);
    }

    //新增检查组,查询所有的检查项
    @Override
    public List<CheckItem> findAll() {
        return checkItemDao.findAll();
    }
}
